package com.egd.imapp.contoller.activity.login;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.egd.imapp.R;
import com.egd.imapp.model.Model;
import com.egd.imapp.model.bean.UserInfo;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;

public class RegisterActivity extends Activity {
    private static final String TAG = "TAG";
    private boolean isRegister  = false;

    private EditText et_register_username;
    private EditText et_register_password;
    private EditText et_register_password2;

    private Button btn_register_register;

    private TextView tv_register_login;
    private int RESULT_CODE_TO_LOGIN = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        // 初始化控件
        initView();

        // 初始化监听
        initListener();
    }

    private void initListener() {
        // 注册
        btn_register_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                regist();
            }
        });

        // 登录 携带注册的数据到登录页面直接进行登录
        tv_register_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isRegister){
                    login();
                }else {
                    Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                    startActivity(intent);
                }
            }
        });

    }

    private void login() {
        String username = et_register_username.getText().toString().trim();
        String password = et_register_password.getText().toString().trim();

        Intent data = new Intent();
        data.putExtra("username", username);
        data.putExtra("password", password);
        Log.e(TAG, "login: RegisterActivity 用户登录："+ username, null);
        setResult(RESULT_CODE_TO_LOGIN, data);

        finish();
    }

    private void regist() {

        // 1 获取输入的用户名和密码
        String username = et_register_username.getText().toString();
        String password = et_register_password.getText().toString();
        String password2 = et_register_password2.getText().toString();

        // 2 校验输入的用户名和密码
        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password) || TextUtils.isEmpty(password2)){
            Log.e(TAG, "regist: 输入的用户名或密码不能为空", null);
            Toast.makeText(RegisterActivity.this, "输入的用户名或密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }else if ( !password.equals(password2)){
            Log.e(TAG, "regist: 前后两次输入的密码不正确", null);
            Toast.makeText(RegisterActivity.this, "前后两次输入的密码不正确", Toast.LENGTH_SHORT).show();
            return;
        }else {

            // 进度条
            final ProgressDialog pb = new ProgressDialog(RegisterActivity.this);
            pb.setMessage("正在注册中");
            pb.show();
            // 3 去服务器注册账号
            Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        // 去环信服务器注册账号
                        EMClient.getInstance().createAccount(username, password);

                        isRegister = true;

                        // 保存数据到数据库
                        Model.getInstance().getUserAccountDao().addAccount(new UserInfo(username));

                        //更新页面显示
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                pb.dismiss();
                                Log.e(TAG, "run: 注册成功！", null);
                                Toast.makeText(RegisterActivity.this, "注册成功！", Toast.LENGTH_SHORT).show();
                            }
                        });
                    } catch (HyphenateException e) {
                        e.printStackTrace();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                pb.dismiss();
                                Log.e(TAG, "run: 注册失败！"+e.toString(), null);
                                Toast.makeText(RegisterActivity.this, "注册失败！请稍后再试..." + e.getMessage(), Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                }
            });
        }

    }

    private void initView() {
        Log.e(TAG, "initView: RegisterActivity",null );
        et_register_username = findViewById(R.id.et_register_account);
        et_register_password = findViewById(R.id.et_register_pwd);
        et_register_password2 = findViewById(R.id.et_register_pwd2);
        btn_register_register = findViewById(R.id.btn_register_register);
        tv_register_login = findViewById(R.id.tv_register_login);
    }
}